package com.niuquan.activitie;

import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TableRow;
import android.widget.TextView;

import com.niuquan.uicomponent.SlidingMenu;

/**
 * Created by DELL on 2015/5/22.
 */
public class MainActivity extends FragmentActivity{

    private ChatFragment chatFragment;
    private ContactsFragment contactsFragment;
    private FindExpertFragment findExpertFragment;
    private NiushiFragment niushiFragment;
    private Fragment fragment;
    private TableRow yijieniuhuo;
    private TableRow yifaniuhuo;
    private TableRow mycoin;
    private TableRow mycollection;
    private SlidingMenu mLeftMenu ;
    private FragmentManager m_fragmentManager;
    private FragmentTransaction m_fragmentTransaction;
    private Fragment[] m_fragments;
    private ImageButton[] m_imagebuttons;
    private TextView[] m_textviews;
    private int index;
    private ImageButton publishtask;
    // 当前fragment的index
    private int currentTabIndex;
    //application
    private ExpertCircle myContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        mLeftMenu = (SlidingMenu) findViewById(R.id.id_menu);
        myContext = (ExpertCircle) getApplicationContext();
        init();
      //  LayoutInflater mLi = LayoutInflater.from(this);
     //   View view1 = mLi.inflate(R.layout.menulayout, null);
        yijieniuhuo = (TableRow)findViewById(R.id.yijieriuhuo);
        yifaniuhuo =(TableRow)findViewById(R.id.yifariuhuo);
        mycoin = (TableRow)findViewById(R.id.mycoin);
        mycollection = (TableRow)findViewById(R.id.mycollection);
        publishtask = (ImageButton)findViewById(R.id.publishtaskbutton);

        yijieniuhuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,HistoryActivity.class);
                startActivity(intent);
            }
        });
        yifaniuhuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,HistoryActivity.class);
                startActivity(intent);
            }
        });
        mycollection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,MyCollectionActivity.class);
                startActivity(intent);
            }
        });
        mycoin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,MycoinActivity.class);
                startActivity(intent);
            }
        });
        publishtask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,publishActivity.class);
                startActivity(intent);
            }
        });
    }
    private void init()
    {
        currentTabIndex = 0;
        index = 0;
        chatFragment = new ChatFragment();
        contactsFragment = new ContactsFragment();
        findExpertFragment = new FindExpertFragment();
        niushiFragment = new NiushiFragment();
        Fragment fragment = myContext.mIMKit.getConversationFragment();
        m_fragments = new Fragment[] { chatFragment, contactsFragment,
                findExpertFragment, niushiFragment };
        m_imagebuttons = new ImageButton[4];
        m_imagebuttons[0] = (ImageButton) findViewById(R.id.niuquantab);
        m_imagebuttons[1] = (ImageButton) findViewById(R.id.niurentab);
        m_imagebuttons[2] = (ImageButton) findViewById(R.id.xunniutab);
        m_imagebuttons[3] = (ImageButton) findViewById(R.id.niushitab);

        m_imagebuttons[0].setSelected(true);
        m_textviews = new TextView[4];
        m_textviews[0] = (TextView) findViewById(R.id.tabtextniuquan);
        m_textviews[1] = (TextView) findViewById(R.id.tabtextniuren);
        m_textviews[2] = (TextView) findViewById(R.id.tabtextxunniu);
        m_textviews[3] = (TextView) findViewById(R.id.tabtextniushi);
        m_textviews[0].setTextColor(getResources().getColor(R.color.choose_tab_color));
        // 添加显示第一个fragment

        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, chatFragment)
                .add(R.id.fragment_container, contactsFragment)
                .add(R.id.fragment_container, findExpertFragment)
                .add(R.id.fragment_container, niushiFragment)
                .hide(contactsFragment).hide(findExpertFragment)
                .hide(niushiFragment).show(chatFragment).commit();

    }
    //get for menu button
    public void MenubarClicked(View view)
    {
        mLeftMenu.toggle();
    }
    //get for tab change button
    public void onTabClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.niurentablayout:
            case R.id.niurentab:
                index = 1;
                break;
            case R.id.niuquantablayout:
            case R.id.niuquantab:
                index = 0;
                break;
            case R.id.xunniutablayout:
            case R.id.xunniutab:
                index = 2;
                break;
            case R.id.niushitablayout:
            case R.id.niushitab:
                index = 3;
                break;
        }
        if (currentTabIndex != index) {
            FragmentTransaction trx = getSupportFragmentManager()
                    .beginTransaction();
            trx.hide(m_fragments[currentTabIndex]);
            if (!m_fragments[index].isAdded()) {
                trx.add(R.id.fragment_container, m_fragments[index]);
            }
            trx.show(m_fragments[index]).commit();
        }
        m_imagebuttons[currentTabIndex].setSelected(false);
        // 把当前tab设为选中状态
        m_imagebuttons[index].setSelected(true);
        m_textviews[currentTabIndex].setTextColor(getResources().getColor(R.color.nochoose_text_color));
        m_textviews[index].setTextColor(getResources().getColor(R.color.choose_tab_color));
        currentTabIndex = index;
    }
}
